<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class GroupController extends Controller
{
	//显示
    public function getIndex(Request $request){
        if($request['name']){
            //搜索分页
            $res = DB::table('group')->where('ClassName','like','%'.$request['name'].'%')->paginate(7);
        }else{
            //所有分页
            $res = DB::table('group')->paginate(7);
        }
       
        $info = $request -> all();
    	// dd($res);
        return view('/group/group',['res'=>$res,'info'=>$info]);
    }

    //添加
    public function getAdd(){
        return view('/group/add');

    }
    //执行添加
    public function postInsert(Request $request){
         $this->validate($request, [
        'ClassName' => 'required|unique:group,CassName',
        'Remark' => 'required',
        ],[
        'ClassName.required'=>'分组名称不能为空',
        'ClassName.unique'=>'分组名称已存在',
        'Remark.required'=>'备注不能为空',
        ]);
    	$res = $request->only(['ClassName','Remark']);
    	// dd($res);
    	// $id = DB::table('group')->insertGetId($res);
        // dd($id);
        DB::table('group')->insert($res);

    	return redirect('/group')->with('res',$res);

    }


    //删除
    public function getDel($id){
    	// dd($id);
    	$res = DB::table('cate')->where('Classid',$id)->get();
    	// dd($res);
    	if(!$res){
    		
    		DB::table('group')->where('ClassId',$id)->delete();
    		return redirect('/group');
    	}else{
    		return redirect('/group')->with('error','分组下有菜单不允许删除');
    		
    	}
    }
    //编辑
    public function getEdit($id){
        $res = DB::table('group')->where('ClassId',$id)->get();
        // dd($res);
    	return view('/group/edit',['res'=>$res]);

    }

    //执行修改
    public function postUpdate(Request $request){
    	$res = $request->only(['ClassId','ClassName','Remark']);
    	// dd($res);
    	DB::table('group')->where('ClassId',$res['ClassId'])->update($res);
    	return redirect('/group');

    }
}
